# -*-* encoding:UTF-8 -*-
# author            : mengy
# date              : 2018/12/14
# python-version    : Python 3.7.0
# description       : 聊天室类

from com.python.demo.mutil_chatting_room.entities.Room import Room


class ChatRoom(Room):
    " 聊天时的房间"

    def add(self, session):
        "新用户进入时，系统群发消息提示"
        session.push(b'Login Success')
        self.broadcast((session.name + '进入了房间！\n').encode('utf-8'))
        self.server.users[session.name] = session
        Room.add(self, session)

    def remove(self, session):
        "用户离开房间"
        Room.remove(self, session)
        self.broadcast((session.name + '离开了房间！\n').encode('utf-8'))

    def do_say(self, session, line):
        "客户端发送消息"
        self.broadcast((session.name + ':' + line + '\n').encode('utf-8'))

    def do_look(self, session, line):
        "查看在线用户"
        session.push(b'All Users:\n')
        for other in self.sessions:
            session.push((other.name + '\n').encode('utf-8'))
